package com.huahang.fashionpress.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huahang.fashionpress.enums.ExceptionEnum;
import com.huahang.fashionpress.exception.ResultException;
import com.huahang.fashionpress.mapper.ImgMapper;
import com.huahang.fashionpress.mapper.ProductMapper;
import com.huahang.fashionpress.model.Product;
import com.huahang.fashionpress.service.ProductService;
import com.huahang.fashionpress.util.FileUtils;
import com.huahang.fashionpress.util.IdUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: 阿俊哥
 * @Date: 2019/8/23 16:18
 * @Version 1.0
 */

@Slf4j
@Transactional
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    ProductMapper productMapper;
    @Resource
    ImgMapper imgMapper;

    public static final int MAINIMGSTATUS=0;

    public static final int IMGSATAUS = 1;
    @Override
    public Page<Product> selectProductList(int productStatus,int size,int pagenum) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_status", productStatus);
        Page<Product> page = new Page<>(pagenum,size);
        Page<Product> pages = new Page<>();
        pages = (Page<Product>) productMapper.selectPageVO(page,productStatus);
        if(page.getSize()==0){
            log.info("【后台商品管理】查询商品列表出错");
            throw  new ResultException(ExceptionEnum.IMG_NOT_EXIT);
        }
        return pages;

    }

    @Override
    public Product selectProductById(String productId) {
        if(productId==null||productId==""){
            log.info("【后台商品管理】查询商品商品编号出错");
            throw  new ResultException(ExceptionEnum.DATA_ERROR);
        }
        Product product = productMapper.selectById(productId);
        if(product==null){
            log.info("【后台商品管理】未找到商品");
            throw  new ResultException(ExceptionEnum.PRODUCT_NOT_EXIT);
        }
        return product;
    }

    @Override
    public List<Product> selectProductByName(String productName) {
        if(productName==null||productName==""){
            log.info("【后台商品管理】管理员名称数据出错");
            throw  new ResultException(ExceptionEnum.DATA_ERROR);
        }
        QueryWrapper<Product> queryWrapper = new QueryWrapper<Product>();
        queryWrapper.eq("product_name", productName);
        List<Product> productList= productMapper.selectList(queryWrapper);
        if(productList.size()==0){
            log.info("【后台商品管理】未找到商品");
            throw  new ResultException(ExceptionEnum.PRODUCT_NOT_EXIT);
        }
        return productList;
    }

    @Transactional
    @Override
    public boolean addProduct(Product product, HttpServletRequest request) {
        String productId = IdUtils.generateProductId();
        product.setProductId(productId);
        int result = productMapper.insert(product);
        //整理图片文件******************
        //获取一个文件
        MultipartFile file = ((MultipartHttpServletRequest)request).getFile("file");
        FileUtils.uploadFile(file,productId,MAINIMGSTATUS, imgMapper);
        //获取多个文件
        String fileName = "files";
        FileUtils.uploadFileList(productId, IMGSATAUS, imgMapper,fileName,request);

        //整理图片文件******************
        if(result>0)
            return true;
        return false;
    }

    @Override
    public void updateProductStatus(String productId) {
        Product product = this.selectProductById(productId);
        if(product.getProductStatus()==0){
            product.setProductStatus(1);
        }
        else {
            product.setProductStatus(0);
        }

        productMapper.updateById(product);

    }

    @Override
    public boolean updateProduct(Product product) {
        int result = productMapper.updateById(product);
        if(result>0)
            return true;
        return false;
    }

    //判断商品类别下是否存在商品
    @Override
    public List<Product> SelectProductByCategoryId(int id) {
        if("0".equals(String.valueOf(id)) || "null".equals(String.valueOf(id)) || id < 0){
            log.info("【商品管理】：查询的商品类别为空");
            throw new ResultException(ExceptionEnum.DATA_ERROR);
        }
        List<Product> productList = productMapper.SelectProductByCategoryId(id);
        return productList;
    }
}
